package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
	"unicode"
)

func isChinese(s string) bool { //仅中文字符
	for _, r := range s {
		if !unicode.Is(unicode.Han, r) {
			return false
		}
	}
	return true
}

func main() {
	file, err := os.Open("dictionary.txt")
	if err != nil {
		fmt.Println("Error opening file:", err)
		return
	}
	defer file.Close()

	newFile, err := os.Create("isChinese.dictionary.txt")
	if err != nil {
		fmt.Println("Error creating file:", err)
		return
	}
	defer newFile.Close()

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := scanner.Text()
		parts := strings.Split(line, " ")

		if len(parts) > 0 {
			firstPart := parts[0]
			// 忽略单字的切片
			if len(firstPart) <= 3 { //UTF8编码的中文字符长度为3
				continue
			}
			if isChinese(firstPart) {
				_, err := newFile.WriteString(firstPart + "\n")
				if err != nil {
					fmt.Println("Error writing to file:", err)
					return
				}
			}
		}
	}

	if err := scanner.Err(); err != nil {
		fmt.Println("Error reading file:", err)
	}
}
